from flask import Flask, redirect, request, jsonify, session
from learn import db, Video, LeadingActor

app = Flask(__name__)
app.secret_key = "asphodel"


@app.route("/video/all", methods=["GET"])
def video():
    i = Video.query.all().first()
    json = jsonify(id=i.id, name=i.name, time=i.time, director=i.director, content=i.content, url=i.url, img_url=i.img_url)
    return json


@app.route("/init")
def init():
    return "hello world"


#
@app.route("/123/<string:username>")
def t123(username):
    return "首付款" + username


# 重定向
@app.route("/wpp")
def wpp():
    return redirect("https://fanyi.baidu.com")


@app.route("/test/my/post", methods=["POST"])
def post():
    my_json = request.get_json()
    get_name = my_json.get("name")
    get_age = my_json.get("age")
    return jsonify(na=get_name, an=get_age)


# 登录
@app.route("/try/login", methods=["POST"])
def login():
    get_data = request.get_json()
    get_name = get_data.get("name")
    session["username"] = get_name
    return jsonify(msg="登陆成功")


# 退出登陆
@app.route("/try/logout", methods=["GET"])
def logout():
    session.clear()
    return jsonify(msg="退出登录")


# 登陆状态
@app.route("/session", methods=["POST"])
def check_session():
    username = session.get("username")
    if username is not None:
        return jsonify(username=username)
    else:
        return jsonify(msg="未登录")


app.run(host="0.0.0.0")
